﻿/**
 *       Project: ServerLib
 *     Copyright: Copyright (C) 2008, Rob Kennedy
 *   Create Date: 2008-07-15 3:40 PM
 * Modified Date: 2008-07-15 3:40 PM    RK  Added comments
 *      Comments: Ignore SA1633.
 *      
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace ServerLib.Settings
{
    using System.Collections.ObjectModel;
    using System.Data;
    using ServerLib.Core;
    using ServerLib.Data;

    /// --------------------------------------------------------------------
    /// Project	 : ServerLib
    /// Class	 : Domain
    /// Inherits : DataRowRecord
    /// --------------------------------------------------------------------
    /// <summary>
    /// A class representation of a Domain database record
    /// </summary>
    /// <authorname>Rob Kennedy</authorname>
    /// <remarks></remarks>
    /// --------------------------------------------------------------------
    public class Domain : DataRowRecord
    {
        #region Private Properties

        /// <summary>
        /// Contains a collection list of associated domain user accounts
        /// </summary>
        private Collection<User> domainUsers = new Collection<User>();

        /// <summary>
        /// Contains a collection list of associated domain aliases
        /// </summary>
        private Collection<Alias> domainAliases = new Collection<Alias>();
        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the unique Domain record Identifier number
        /// </summary>
        public int DomainID
        {
            get
            {
                return Types.FixInteger(this.InternalDataRow["DomainID"]);
            }
        }

        /// <summary>
        /// Gets or sets the Domain record name value
        /// </summary>
        public string DomainName
        {
            get
            {
                return Types.FixString(this.InternalDataRow["DomainName"]);
            }

            set
            {
                if (this.InternalDataRow.RowState == System.Data.DataRowState.Unchanged)
                {
                    this.InternalDataRow.BeginEdit();
                }

                this.InternalDataRow["DomainName"] = value;
            }
        }

        /// <summary>
        /// Gets a collection of associated domain user records
        /// </summary>
        public Collection<User> Users
        {
            get
            {
                if (this.Connection != null)
                {
                    this.domainUsers.Clear();

                    bool wasPreviouslyOpen = this.Connection.IsConnected;

                    if (!wasPreviouslyOpen)
                    {
                        this.Connection.Open();
                    }

                    if (this.Connection.IsConnected)
                    {
                        DataTable resultsTable = this.Connection.SelectQuery(SqlCommandStringBuilder.BuildSelect("Users", null, string.Format("WHERE DomainID={0}", this.DomainID), "UserName"));
                        if (resultsTable != null)
                        {
                            foreach (DataRow userRow in resultsTable.Rows)
                            {
                                this.domainUsers.Add(new User(userRow));
                            }
                        }
                    }
                    
                    if (!wasPreviouslyOpen)
                    {
                        this.Connection.Close();
                    }
                }

                return this.domainUsers;
            }
        }

        /// <summary>
        /// Gets a collection of associated domain alias records
        /// </summary>
        public Collection<Alias> Aliases
        {
            get
            {
                if (this.Connection != null)
                {
                    this.domainAliases.Clear();

                    bool wasPreviouslyOpen = this.Connection.IsConnected;

                    if (!wasPreviouslyOpen)
                    {
                        this.Connection.Open();
                    }

                    if (this.Connection.IsConnected)
                    {
                        DataTable resultsTable = this.Connection.SelectQuery(SqlCommandStringBuilder.BuildSelect("Aliases", null, string.Format("WHERE DomainID={0}", this.DomainID), "AliasName"));

                        if (resultsTable != null)
                        {
                            foreach (DataRow aliasRow in resultsTable.Rows)
                            {
                                this.domainAliases.Add(new Alias(aliasRow));
                            }
                        }
                    }

                    if (!wasPreviouslyOpen)
                    {
                        this.Connection.Close();
                    }
                }

                return this.domainAliases;
            }
        }

        #endregion

        #region Public Constructors

        /// <summary>
        /// Initializes the Domain object with a new DataRow class
        /// </summary>
        /// <param name="dataConnection">The IDatabase object to connect with</param>
        public Domain(IDatabase dataConnection)
            : base(dataConnection)
        {
        }

        /// <summary>
        /// Initializes the Domain object with a new DataRow class
        /// </summary>
        /// <param name="dataConnection">The IDatabase object to connect with</param>
        /// <param name="tableName">The Record table name to load into the Record object</param>
        /// <param name="fieldName">The Record table field name name used to identify when loading the recordId</param>
        /// <param name="recordId">The unique record ID number to load into the Record object</param>
        public Domain(IDatabase dataConnection, string tableName, string fieldName, int recordId)
            : base(dataConnection, tableName, fieldName, recordId)
        {
        }

        /// <summary>
        /// Initializes the Domain object with a new DataRow class
        /// </summary>
        /// <param name="dataTable">The DataTable object to retrieve a new DataRow object from</param>
        public Domain(DataTable dataTable)
            : base(dataTable)
        {
        }
        
        /// <summary>
        /// Initializes the Domain object with a new DataRow class
        /// </summary>
        /// <param name="dataRow">The DataRow object to initialize the Domain object with</param>
        public Domain(DataRow dataRow)
            : base(dataRow)
        {
        }
        #endregion
    }
}
